package com.xhwl.logistics.service.impl.http;

import com.alibaba.fastjson.JSON;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.cent.VehicleParkingInfoBO;
import com.xhwl.logistics.dto.http.CarportHaiKangDTO;
import com.xhwl.logistics.dto.http.CommonHaiKangDTO;
import com.xhwl.logistics.dto.http.ParkingLotInfoHaiKangDTO;
import com.xhwl.logistics.dto.http.RoadwayHaiKangDTO;
import com.xhwl.logistics.dto.http.query.QueryCarportHaiKangDTO;
import com.xhwl.logistics.dto.http.query.QueryRoadwayHaiKangDTO;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
//@Service
//1：对海康的http请求，后续可以考虑不用海康提供的工具，用ak/sk的方式
//2：发http请求的方式，可以后续可以归纳，设置请求相关信息代码有重复了
public class HaiKangHttpServiceImpl {

    private synchronized void setHaiKangHttpConfig(VehicleParkingInfoBO vehicleParkingInfoBO) {
        ArtemisConfig.host = vehicleParkingInfoBO.getHost();
        ArtemisConfig.appKey = vehicleParkingInfoBO.getAppId();
        ArtemisConfig.appSecret = vehicleParkingInfoBO.getAppSecret();
    }

    private void haiKangResponseHandle(CommonHaiKangDTO commonHaiKangDTO) {
        if (!"0".equals(commonHaiKangDTO.getCode())) {
            throw new BusinessException("访问海康的http接口，返回的code不是代表成功的0");
        }
    }

    public List<ParkingLotInfoHaiKangDTO> getParkingLotInfo(VehicleParkingInfoBO vehicleParkingInfoBO) {
        setHaiKangHttpConfig(vehicleParkingInfoBO);

        System.out.println("http hai kang getParkingLotInfo!");

        final String pathRoot = "/artemis";
        String url = "/api/resource/v1/park/parkList";

        Map<String, String> path = new HashMap<>();
        path.put("https://", pathRoot+url);

        String contentType = "application/json";

        String body = "{}";

        String bodyString = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
        System.out.println("bodyString: " + bodyString);

        CommonHaiKangDTO commonHaiKangDTO = JSON.parseObject(bodyString, CommonHaiKangDTO.class);
        haiKangResponseHandle(commonHaiKangDTO);
        System.out.println("commonHaiKangDTO: " + commonHaiKangDTO);

        List<ParkingLotInfoHaiKangDTO> parkingLotInfoHaiKangDTOList = JSON.parseArray(commonHaiKangDTO.getData(), ParkingLotInfoHaiKangDTO.class);
        parkingLotInfoHaiKangDTOList.forEach(parkingLotInfoHaiKangDTO -> {
            System.out.println("parkingLotInfoHaiKangDTO: " + parkingLotInfoHaiKangDTO);
        });

        return parkingLotInfoHaiKangDTOList;
    }

    public List<CarportHaiKangDTO> getCarport(VehicleParkingInfoBO vehicleParkingInfoBO) {
        setHaiKangHttpConfig(vehicleParkingInfoBO);

        System.out.println("http hai kang getCarport!");

        final String pathRoot = "/artemis";
        String url = "/api/pms/v1/park/remainSpaceNum";

        Map<String, String> path = new HashMap<>();
        path.put("https://", pathRoot+url);

        String contentType = "application/json";

        String body = "{}";

        String bodyString = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
        System.out.println("bodyString: " + bodyString);

        CommonHaiKangDTO commonHaiKangDTO = JSON.parseObject(bodyString, CommonHaiKangDTO.class);
        haiKangResponseHandle(commonHaiKangDTO);
        System.out.println("commonHaiKangDTO: " + commonHaiKangDTO);

        List<CarportHaiKangDTO> carportHaiKangDTOList = JSON.parseArray(commonHaiKangDTO.getData(), CarportHaiKangDTO.class);
        carportHaiKangDTOList.forEach(carportHaiKangDTO -> {
            System.out.println("carportHaiKangDTO: " + carportHaiKangDTO);
        });

        return carportHaiKangDTOList;
    }
    public CarportHaiKangDTO getCarport(VehicleParkingInfoBO vehicleParkingInfoBO, QueryCarportHaiKangDTO queryCarportHaiKangDTO) {
        log.info("调用海康车场查询车位数量的接口，根据车场的id");

        setHaiKangHttpConfig(vehicleParkingInfoBO);

        final String pathRoot = "/artemis";
        String url = "/api/pms/v1/park/remainSpaceNum";

        Map<String, String> path = new HashMap<>();
        path.put("https://", pathRoot+url);

        String contentType = "application/json";

        String body = JSON.toJSONString(queryCarportHaiKangDTO);

        String bodyString = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
        System.out.println("bodyString: " + bodyString);

        CommonHaiKangDTO commonHaiKangDTO = JSON.parseObject(bodyString, CommonHaiKangDTO.class);
        haiKangResponseHandle(commonHaiKangDTO);
        log.info("commonHaiKangDTO: " + commonHaiKangDTO);

        List<CarportHaiKangDTO> carportHaiKangDTOList = JSON.parseArray(commonHaiKangDTO.getData(), CarportHaiKangDTO.class);
        carportHaiKangDTOList.forEach(carportHaiKangDTO -> {
            log.info("carportHaiKangDTO: " + carportHaiKangDTO);
        });

        if (1!=carportHaiKangDTOList.size()) {
            throw new IllegalStateException("查询单个车场的车位数量时，接口返回关于车位的数据存在多个或者为空");
        }

        return carportHaiKangDTOList.get(0);
    }

    public List<RoadwayHaiKangDTO> getRoadways(VehicleParkingInfoBO vehicleParkingInfoBO, QueryRoadwayHaiKangDTO queryRoadwayHaiKangDTO) {
        setHaiKangHttpConfig(vehicleParkingInfoBO);

        System.out.println("http hai kang getRoadways!");

        final String pathRoot = "/artemis";
        String url = "/api/resource/v1/roadway/roadwayList";

        Map<String, String> path = new HashMap<>();
        path.put("https://", pathRoot+url);

        String contentType = "application/json";

        String body = JSON.toJSONString(queryRoadwayHaiKangDTO);
        System.out.println("body: " + body);

        String bodyString = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
        System.out.println("bodyString: " + bodyString);

        CommonHaiKangDTO commonHaiKangDTO = JSON.parseObject(bodyString, CommonHaiKangDTO.class);
        haiKangResponseHandle(commonHaiKangDTO);
        System.out.println("commonHaiKangDTO: " + commonHaiKangDTO);

        List<RoadwayHaiKangDTO> roadwayHaiKangDTOList = JSON.parseArray(commonHaiKangDTO.getData(), RoadwayHaiKangDTO.class);
        roadwayHaiKangDTOList.forEach(roadwayHaiKangDTO -> {
            System.out.println("roadwayHaiKangDTO: " + roadwayHaiKangDTO);
        });

        return roadwayHaiKangDTOList;
    }


}
